package com.hy.config;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:database.properties")
public class DruidDbConfig {

	private Logger logger = LoggerFactory.getLogger(DruidDbConfig.class);
	
	@Value("${jdbc.url}")
    private String dbUrl;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    
    @Value("${druid.filters}")
    private String filters;
    
    @Value("${druid.initialSize:1}")//设置默认值
    private int initialSize;
    
    @Value("${druid.minIdle:1}")//设置默认值
    private int minIdle;
    
    @Value("${druid.maxActive:1}")//设置默认值
    private int maxActive;
    
    @Value("${druid.maxWait:50000}")//设置默认值
    private int maxWait;
    
    @Value("${druid.timeBetweenEvictionRunsMillis:50000}")//设置默认值
    private int timeBetweenEvictionRunsMillis;
    
    @Value("${druid.minEvictableIdleTimeMillis:50000}")//设置默认值
    private int minEvictableIdleTimeMillis;
    
    @Value("${druid.validationQuery:select 'x'}")
    private String validationQuery;
    
    @Value("${druid.testWhileIdle:false}")
    private boolean testWhileIdle;
    
    @Value("${druid.testOnBorrow:false}")
    private boolean testOnBorrow;
    
    @Value("${druid.testOnReturn:false}")
    private boolean testOnReturn;
    
    @Value("${druid.poolPreparedStatements:false}")
    private boolean poolPreparedStatements;
    
    @Value("${druid.maxOpenPreparedStatements:20}")//设置默认值
    private int maxOpenPreparedStatements;
    
    @Bean
    @Primary  //在同样的DataSource中，首先使用被标注的DataSource
    public DataSource dataSource(){
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxOpenPreparedStatements);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }
//        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
}
